/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bo;

import org.ipti.br.tag.bean.SessionBean;
import com.itextpdf.text.Document;
import org.ipti.br.tag.dao.WeightAndHeightFacade;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Personage;
import org.ipti.br.tag.entity.Unity;
import org.ipti.br.tag.entity.WeightAndHeight;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.ipti.br.tag.pdf.WeightAndHeightPdf;
import org.ipti.br.tag.util.LoggerUtil;
import org.ipti.br.tag.bean.dataManipulation.WeightAndHeightAuxiliator;

/**
 *
 * @author FelipeMSX
 */
@Stateless
public class WeightAndHeightBO {

    @Inject private WeightAndHeightFacade measureFacade;
    @Inject private PersonageBO personageBO;
    @Inject private UnityBO unityBO;
    @Inject private ActorBO actorBO;
    @Inject private HumanBO humanBO;

    public void create(WeightAndHeight measure) {
        measureFacade.create(measure);
    }

    public void edit(WeightAndHeight measure) {
        measureFacade.edit(measure);
    }

    public void remove(WeightAndHeight measure) {
        measureFacade.remove2(measure);
    }

    public WeightAndHeight find(Object id) {
        return measureFacade.find(id);
    }

    public List<WeightAndHeight> findAll() {
        return measureFacade.findAll();
    }

    public WeightAndHeightFacade getMeasureFacade() {
        return measureFacade;
    }

    public void setMeasureFacade(WeightAndHeightFacade measureFacade) {
        this.measureFacade = measureFacade;
    }

    public List<WeightAndHeight> modifiedPerfFreqBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        return getMeasureFacade().modifiedPerfFreqBetweenDate(initialDate, finalDate, serialNumber);
    }

    public WeightAndHeight getLastMeasure(Human human) {
        return measureFacade.getLastMeasure(human);
    }

    public List<WeightAndHeight> findAllWeightAndHeight(Unity unity, Campaign campaign) {
        return measureFacade.findAllWeightAndHeight(unity, campaign);
    }

    public List<WeightAndHeightAuxiliator> getTableDataWH(Campaign campaign, Unity unity) {
        List<WeightAndHeightAuxiliator> WHList = new ArrayList<>();
        List<Human> humanList = new ArrayList<>();
        List<WeightAndHeight> weightAndHeightList = new ArrayList<>();
        WeightAndHeightAuxiliator wahAuxiliator;
        Personage personage = personageBO.find((long) 623403461);
        List<Unity> unities = unityBO.unityChildrenPerOrganization(unity, personage.getOrganization());

        if (!unities.isEmpty()) {
            for (Unity u : unities) {
                for (Human humanElement : humanBO.getActivatedHumans(unity)) {
                    humanList.add(humanElement);
                }
                for (WeightAndHeight wahElement : findAllWeightAndHeight(u, campaign)) {
                    weightAndHeightList.add(wahElement);
                }
            }
        } else {
            for (Human humanElement : humanBO.getActivatedHumans(unity)) {
                humanList.add(humanElement);
            }
            for (WeightAndHeight wahElement : findAllWeightAndHeight(unity, campaign)) {
                weightAndHeightList.add(wahElement);
            }
        }

        for (Human humanElement : humanList) {
            wahAuxiliator = new WeightAndHeightAuxiliator();
            wahAuxiliator.setHuman(humanElement);
            boolean found = false;

            for (WeightAndHeight elementW : weightAndHeightList) {
                if (elementW.getHuman().equals(humanElement)) {
                    wahAuxiliator.setWeightAndHeight(elementW);
                    found = true;
                    wahAuxiliator.setNotExistsDataBase(false);
                }
            }

            if (!found) {
                wahAuxiliator.setWeightAndHeight(new WeightAndHeight());
                wahAuxiliator.setNotExistsDataBase(true);
            }
            WHList.add(wahAuxiliator);
        }

        if (!WHList.isEmpty()) {
            Collections.sort(WHList);
        }
        return WHList;
    }

    public List<WeightAndHeightAuxiliator> getDataTableWaH(Campaign campaign, Unity unity) {
        List<WeightAndHeightAuxiliator> WHList = new ArrayList<>();
        List<Human> humans = new ArrayList<>();
        
        List<Long> unities = unityBO.unityChildrenPerOrganization2(unity.getId(), 2040438459L);

        if (!unities.isEmpty()) {
            for (Long unityID : unities) {
                humans.addAll(humanBO.getHumanDAO().getActivatedHumans(unityBO.find(unityID)));
            }
        } else {
            humans.addAll(humanBO.getHumanDAO().getActivatedHumans(unity));
        }
        WeightAndHeightAuxiliator aux;
        for (Human human : humans) {
            aux = new WeightAndHeightAuxiliator(human, getMeasureFacade().getLastMeasure(human), false);
            if (aux.getWeightAndHeight() == null) {
                aux.setNotExistsDataBase(true);
                aux.setWeightAndHeight(new WeightAndHeight());
                aux.getWeightAndHeight().setHuman(human);
                aux.getWeightAndHeight().setCampaign(campaign);
            }
            WHList.add(aux);
        }
        return WHList;
    }
    public boolean generatePdf(SessionBean sessionBean, Campaign campaign, Unity unity, List<WeightAndHeightAuxiliator> WHList) {
        if (!WHList.isEmpty()) {
            try {
                new WeightAndHeightPdf().run(WHList, sessionBean.getLoggedActor().getUnity(), unity, campaign);
                return true;
            } catch (Exception ex) {
                Logger.getLogger(WeightAndHeightBO.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        } else {
            return false;
        }
    }

    public void saveAll(List<WeightAndHeightAuxiliator> WHList, Campaign campaign) {

        WeightAndHeight w;
        for (WeightAndHeightAuxiliator element : WHList) {
            if (element.getWeightAndHeight().getHeight() != 0 || element.getWeightAndHeight().getWeight() != 0) {
                if (!element.isNotExistsDataBase()) {
                    edit(element.getWeightAndHeight());
                } else {
                    w = element.getWeightAndHeight();
                    w.setCampaign(campaign);
                    w.setHuman(element.getHuman());
                    w.setCollectionDate(Calendar.getInstance());
                    create(w);
                }
                measureFacade.flush();
            }
        }
    }

}
